package com.base.cn.platform.os.controller.user.user;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.course.CourseType;
import com.base.cn.platform.os.common.utils.web.WebUtils;
import com.base.cn.platform.os.entity.course.cou.Course;
import com.base.cn.platform.os.entity.order.order.OrderCondition;
import com.base.cn.platform.os.service.course.cou.CourseService;
import com.base.cn.platform.os.service.course.cou.buy.CourseBuyRecordService;
import com.base.cn.platform.os.service.course.cou.record.CourseLearnFlowService;
import com.base.cn.platform.os.service.course.cou.record.CoursePlayRecordService;
import com.base.cn.platform.os.service.course.qa.question.QaQuestionService;
import com.base.cn.platform.os.service.course.qa.record.QaGainAttendRecordService;
import com.base.cn.platform.os.service.exam.extend.ExamUserExpandService;
import com.base.cn.platform.os.service.exam.record.ExamRecordService;
import com.base.cn.platform.os.service.manage.order.goods.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台用户管理用户详情
 * @author ll
 * @date 2018/7/3 9:49
 */
@Controller
@RequestMapping("/admin/user/cus")
public class AdminUserExtendedDataController extends AdminBaseController {
    @Autowired
    private QaQuestionService qaQuestionService;
    @Autowired
    private CourseBuyRecordService courseBuyRecordService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private QaGainAttendRecordService attendRecordService;
    @Autowired
    private CoursePlayRecordService coursePlayRecordService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private ExamRecordService examRecordService;
    @Autowired
    private CourseLearnFlowService courseLearnFlowService;
    @Autowired
    private ExamUserExpandService examUserExpandService;

    /**
     * 搜索购买课程记录
     * @param courseBuyRecord 用户Id
     * @return
     */
    @RequestMapping("/ajax/queryCourseBuyRecord")
    public String queryCourseBuyRecord(Model model,@RequestBody Map<String,Object> courseBuyRecord,
                                       @RequestParam(value = "currentPage",required = false,defaultValue = "1")int currentPage){
        Map<String,Object> dataMap = courseBuyRecordService.findCourseBuyRecordPage(courseBuyRecord,currentPage,20,true,false,true,false,false,false,false);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/user/user/user_ajax_buy_course_list");
    }

    /**
     * 查看学员订单详情
     * @param model
     * @param orderId
     * @return
     */
    @RequestMapping("/queryOrderByOrderId")
    public String queryOrderByOrderId(Model model,
                                      @RequestParam("orderId")BigDecimal orderId){
        OrderCondition condition = new OrderCondition();
        condition.setId(orderId);
        condition.setSetUserData(true);
        condition.setSetDetailsData(true);
        condition.setSetShopData(true);
        condition.setSetOrderBook(false);
        Map<String, Object> map = orderService.findOrderInfo(condition);
        model.addAttribute("userOrderIfo",map);
        return resultPage("/user/user/userOrderInfo");
    }

    /**
     * ajax提问列表
     * @param model
     * @param params
     * @return
     */
    @RequestMapping("/ajax/queryQuestionByUserId")
    public String  queryQuestionByUserId(Model model,
                                         @RequestBody Map<String,Object> params,
                                         @RequestParam("currentPage")int currentPage){
        String str =params.get("userId").toString();
        BigDecimal userId = BigDecimal.valueOf(Long.parseLong(str));
        params.put("selectFrom",1);//区分前后台查询，1是后台查询列表，2是前台查询列表
        Map<String, Object> dataMap = qaQuestionService.queryQuestionListPage(currentPage, 10, params, userId,false, true, false, false, false, false, false,true);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/user/user/user-question-ajax-list");
    }


    /**
     * 收益记录
     * @param model
     * @param params
     * @param currentPage
     * @return
     */
    @RequestMapping("/ajax/getEarningList")
    public String getEarningList(Model model,
                                 @RequestBody Map<String,Object> params,
                                 @RequestParam("currentPage")int currentPage){
        params.put("userType",3);
        Map<String, Object> dataMap = attendRecordService.queryGainAttendRecordListPage(currentPage, params, true, true);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/user/user/earning-ajax-list");
    }

    /**
     * 学习记录
     * @param model
     * @param params
     * @param currentPage
     * @return
     */
    @RequestMapping("/queryCoursePlayRecord")
    public String queryCoursePlayRecord(Model model,
                                        @RequestBody Map<String,Object> params,
                                        @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        Boolean setCreator = false;
        if (ObjectUtils.isNotEmpty(params)) {
            setCreator=params.get("setCreator") == null ? false : Boolean.parseBoolean(params.get("setCreator").toString());
        }
        model.addAttribute("setCreator", setCreator);
        Map<String,Object> dataMap = coursePlayRecordService.findCoursePlayRecordPage(params,currentPage,10,true,setCreator,true,false);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/user/user/course_ajax_play_record");
    }


    /**
     * 查看课程详情
     * @param model
     * @param courseId
     * @param userId
     * @return
     */
    @RequestMapping("/queryCourseInfoByCourseId")
    public String queryCourseInfoByCourseId(Model model,
                                            @RequestParam("courseId")BigDecimal courseId,
                                            @RequestParam("userId")BigDecimal userId){
        Course dataMap =courseService.findToBuyCourseManyById(courseId,userId,false,false,true,true,false,false,false);
        if(ObjectUtils.isNotEmpty(dataMap)){
            if (dataMap.getCourseTypeKey().equals(CourseType.COURSE_TYPE_PACKAGE.getKey())){
                Map<String,Object> packList = courseService.findPackageInfo(courseId);
                model.addAttribute("packList",packList);
            }
        }
        model.addAttribute("course",dataMap);
        return resultPage("/user/user/preview_course");
    }

    /**
     *
     * @param model
     * @param params
     * @param currentPage
     * @return
     */
    @RequestMapping("/queryExamRecord")
    public String queryExamRecord(Model model,
                                  @RequestBody Map<String,Object> params,
                                  @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        Map<String,Object> examRecordMap =examRecordService.findExamRecordListPage(params,currentPage,10);
        model.addAttribute("dataMap",examRecordMap);
        return resultPage("/user/user/user_exam_ajax_record");
    }
    /**
     * 打开章节学习明细
     * @param model
     * @param userId
     * @param buyCourseId
     * @param courseId
     * @param catalogId
     * @return
     */
    @RequestMapping("/queryCoursePlayDetail")
    public String queryCoursePlayDetailPage(Model model,
                                            @RequestParam("userId") BigDecimal userId, @RequestParam("buyCourseId") BigDecimal buyCourseId, @RequestParam("courseId") BigDecimal courseId, @RequestParam("catalogId") BigDecimal catalogId){
        model.addAttribute("userId",userId);
        model.addAttribute("buyCourseId",buyCourseId);
        model.addAttribute("courseId",courseId);
        model.addAttribute("catalogId",catalogId);
        return resultPage("/user/user/course_play_detils_record");
    }

    /**
     * 查询章节学习明细
     * @param model
     * @param params
     * @param currentPage
     * @return
     */
    @RequestMapping("/queryCoursePlayDetailPage")
    public String queryCoursePlayDetailPage(Model model,
                                            @RequestBody  Map<String,Object> params,
                                            @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        Map<String,Object> dataMap = courseLearnFlowService.queryCoursePlayDetailPage(params,currentPage,10,true,true);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/user/user/course_ajax_play_detils_record");
    }

    /**
     * 查询章节学习明细学习时间（用于生成折线图）
     * @return
     */
    @RequestMapping("/selectLearnTimeMap")
    @ResponseBody
    public List<Map<String,Object>> selectLearnTimeMap(@RequestBody  Map<String,Object> params){

        if(ObjectUtils.isEmpty(params.get("endCreateTime"))&&ObjectUtils.isEmpty(params.get("beginCreateTime"))){
            Date dateAfterDays = DateUtils.getDateAfterDays(new Date(), -6);
            params.put("beginCreateTime",DateUtils.format(dateAfterDays,"YYYY-MM-dd"));
            params.put("endCreateTime",DateUtils.format(new Date(),"YYYY-MM-dd"));
        }
        return courseLearnFlowService.selectLearnTimeMap(params);
    }

    /**
     * 查询某天用户当天的学习时间
     * @return
     */
    @RequestMapping("/selectOneDayTime")
    @ResponseBody
    public Integer selectOneDayTime(@RequestParam("userId") BigDecimal userId){
        Map<String,Object>  params = new HashMap<>();
        params.put("userId",userId);
        params.put("createTime",DateUtils.format(new Date()));
        Integer integer = courseLearnFlowService.selectOneDayTime(params);
        if(ObjectUtils.isEmpty(integer)){
            return 0;
        }
        return integer ;
    }


    /**
     * 导出学习记录数据
     * @param userId

     * @return
     */
    @RequestMapping("/downloadExcelDetailsList")
    public ResponseEntity<byte[]> downloadExcelDetailsList(@RequestParam("userId") BigDecimal userId,@RequestParam(value = "beginUpdateTime",required =  false) String beginUpdateTime,@RequestParam(value = "endUpdateTime",required =  false) String endUpdateTime) throws Exception {
        Map<String,Object> map  =new HashMap<>();
        map.put("userId",userId);
        map.put("beginUpdateTime",beginUpdateTime);
        map.put("endUpdateTime",endUpdateTime);
        return coursePlayRecordService.downloadExcelDetailsList(map,2000);
    }

    /**
     * @param model
     * @param userId
     * @return
     */
    @RequestMapping("/queryQuestionRecord")
    public String queryQuestionRecord(Model model, @RequestParam String userId) {
        Map<String, Object> examRecordMap = examUserExpandService.findExamUserExpandByUserId(new BigDecimal(userId));
        model.addAttribute("dataMap", examRecordMap);
        return resultPage("/user/user/user_question_ajax_record");
    }

    /**
     * 查询课程统计数据
     *
     * @param model
     * @param courseBuyRecord
     * @return
     */
    @RequestMapping("/queryCourseCount")
    public String queryCourseCount(Model model, @RequestBody Map<String, Object> courseBuyRecord,
                                   @RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage) {
        //课程记录数据
        Map<String, Object> courseBuyRecordPage = courseBuyRecordService.findCourseBuyRecordPage(courseBuyRecord, currentPage, 20, true, false, false, true, true, false, false);
        model.addAttribute("dataMap",courseBuyRecordPage);
        return resultPage("/user/user/user_course_ajax_count");
    }

}
